﻿using System;
using System.ComponentModel;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace $rootnamespace$.$subnamespace$
{
    [ToolboxItemAttribute(false)]
    public class $safeitemrootname$ : WebPart
    {
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            try
            {
                // Create the MVC pattern
                InitMvcPattern();
            }
            catch (Exception exception)
            {
                this.Controls.Clear();
                var errorLabel = new Label();
                this.Controls.Add(errorLabel);
                errorLabel.Text = String.Format("Error: {0}, Stack trace: {1}", exception.Message, exception.StackTrace);
            }

        }

        /// <summary>
        /// Initializes a MVC pattern
        /// </summary>
        void InitMvcPattern()
        {
            // Create the model. The model handles domain logic.
            var model = new $safeitemrootname$Model();

            // Create a view. Many views can be created if needed. A view renders the user interface.
            var view = new $safeitemrootname$View(model);

            // Create the controller. The controller is passed to the model and the view. The controller handles user interaction events
            var controller = new $safeitemrootname$Controller(model, view);

            // We pass the view into the web part container
            this.Controls.Add(view);
        }

    }
}
